package org.imaginedays.androidall.app;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Logger;

import org.imaginedays.androidall.error.AndroidAllException;
import org.imaginedays.androidall.error.AndroidAllParseException;
import org.imaginedays.androidall.http.AndroidAllHttpApi;
import org.imaginedays.androidall.types.UploadFile;
import org.imaginedays.androidall.types.User;

public class AndroidAll {

	private static final Logger LOG = Logger.getLogger(AndroidAll.class
			.getCanonicalName());

	private AndroidAllHttpApi mAndroidAllHttpApi;

	public AndroidAll(AndroidAllHttpApi mAndroidAllHttpApi) {
		super();
		this.mAndroidAllHttpApi = mAndroidAllHttpApi;
	}

	/**
	 * http post 请求
	 * 
	 * @param uId
	 * @param uName
	 * @param uAge
	 * @param uAddress
	 * @return
	 * @throws AndroidAllParseException
	 * @throws AndroidAllException
	 * @throws IOException
	 */
	public User addUserPost(String uId, String uName, String uAge,
			String uAddress) throws AndroidAllParseException,
			AndroidAllException, IOException {
		return mAndroidAllHttpApi.addUserPost(uId, uName, uAge, uAddress);
	}

	/**
	 * http get 请求
	 * 
	 * @param uId
	 * @param uName
	 * @param uAge
	 * @param uAddress
	 * @return
	 * @throws AndroidAllParseException
	 * @throws AndroidAllException
	 * @throws IOException
	 */
	public User addUserGet(String uId, String uName, String uAge,
			String uAddress) throws AndroidAllParseException,
			AndroidAllException, IOException {
		return mAndroidAllHttpApi.addUserGet(uId, uName, uAge, uAddress);
	}

	public User addUserInfo(String uId, String uName, String uAge,
			String uAddress, String uPhotoPath) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(User.UID, uId);
		params.put(User.UNAME, uName);
		params.put(User.UAGE, uAge);
		params.put(User.UADDRESS, uAddress);
		File userPhoto = new File(uPhotoPath);
		UploadFile avatarFile = new UploadFile(userPhoto.getName(), userPhoto,
				"img", "image/jpeg");
		return (User) mAndroidAllHttpApi.uploadUserInfo(params, avatarFile);
	}
	
	public User addUserInfo(String uId, String uName, String uAge,
			String uAddress) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(User.UID, uId);
		params.put(User.UNAME, uName);
		params.put(User.UAGE, uAge);
		params.put(User.UADDRESS, uAddress);
		return (User) mAndroidAllHttpApi.uploadUserInfo(params);
	}

}
